package Reeks1.VanRansbeeckKevin;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.util.Random;

import javax.swing.*;


class Swing extends JFrame {

    Container c;
    JLabel label1,label2,label3,label4,imagelabel;
    JTextField veld1,veld2,veld3,veld4,veld5;
    JButton button1,button2,button3,button4,button5;
    JPanel panel1,panel2,panel3,imagepanel,allpanel;
    JRadioButton yesButton,noButton;
    Image img;
    int BOVENGRENS;
    int aantal_gokken = 0;
    int randomnumber = 0;
    Random generator = new Random();
    URL iconURL;

    public Swing() {

        super("VGO <-> Opgave #1");
        c = getContentPane();
        c.setLayout(new GridLayout(1, 2));


        label1=new JLabel("Geef de bovengrens in (minstens 10):");
        label2=new JLabel("Uw gok:");
        label3=new JLabel("Uw laatste gokken:");
        label4=new JLabel();
        
        veld1=new JTextField(5);
        veld2=new JTextField(5); veld2.setEnabled(false);

        button1=new JButton("START");
        button2=new JButton("GOK"); button2.setEnabled(false);

        allpanel=new JPanel(new GridLayout(3, 1));

        imagepanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        imagepanel.setSize(300,300);

        yesButton = new JRadioButton("Toon een foto als inspiratie!", false);
        noButton = new JRadioButton("Weg ermee!", true);
        ButtonGroup bgroup = new ButtonGroup();
        bgroup.add(yesButton); bgroup.add(noButton);

        imagepanel.add(yesButton); imagepanel.add(noButton);

        imagelabel = new JLabel();

        imagepanel.add(imagelabel);
        try {
            iconURL = ClassLoader.getSystemResource("images/foto.jpg");
            System.out.println(iconURL);
            imagelabel.setIcon(new ImageIcon(iconURL));
        } catch (Exception e) {}
                //"images/foto.jpg"));
        
        imagelabel.setVisible(false); // Verrassingseffect

        yesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                imagelabel.setVisible(true);
                yesButton.setEnabled(false);
                noButton.setEnabled(true);
            }
        });
        noButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                imagelabel.setVisible(false);
                yesButton.setEnabled(true);
                noButton.setEnabled(false);
            }
        });
/*
        File imgF = new File("images/foto.jpg");
        try {
            JOptionPane.showMessageDialog(null, "About to load " + imgF.getCanonicalPath() +
                    " exists=" + imgF.exists());
        } catch (Exception e) {
            e.printStackTrace();
        }
*/

        panel1=new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel2=new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel3=new JPanel(new GridLayout(2, 1)); // 2 rijen - 1 kolom (onder elkaar dus gwn)

        JMenuBar menuBar = new JMenuBar();
        menuBar.setPreferredSize(new Dimension(1500,25));

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenuItem resetMenu = new JMenuItem("Start Over");
        

        fileMenu.add(resetMenu);
        fileMenu.add(new JLabel(iconURL.toString()));
        fileMenu.add(new JLabel(new ImageIcon(iconURL)));

        resetMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                veld1.setEnabled(true); veld1.setText("");
                button1.setEnabled(true);
                veld2.setEnabled(false); veld2.setText("");
                button2.setEnabled(false);
                label3.setText("Uw laatste gokken:");
                label4.setText("");
            }
        });

        setJMenuBar(menuBar);
        
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e1) {
                try {
                    BOVENGRENS = Integer.parseInt(veld1.getText());
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Uw bovengrens moet een geldig getal zijn!");
                }
                if(BOVENGRENS >= 10) {
                    veld2.setEnabled(true);
                    button2.setEnabled(true);
                    veld1.setEnabled(false);
                    button1.setEnabled(false);
                    randomnumber = generator.nextInt(BOVENGRENS);
                }
        }});

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int gok = 0;
                try {
                    gok = Integer.parseInt(veld2.getText());
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "U moet een geldig cijfer ingeven!");
                }
                if(gok >= 0 && gok <= BOVENGRENS) {
                    aantal_gokken++;
                    if(gok == randomnumber) {
                        JOptionPane.showMessageDialog(null, "GEWONNEN");
                        label3.setText(label3.getText() + gok + " !");
                            label4.setForeground(Color.GREEN);
                        label4.setText("Gewonnen in " + aantal_gokken + " stappen");
                    } else {
                        label3.setText(label3.getText() + gok);
                        if(gok > randomnumber) {
                            label3.setText(label3.getText() + " (-) ");
                            label4.setForeground(Color.RED);
                            label4.setText("Uw gok moet lager zijn dan " + gok);
                        } else {
                            label3.setText(label3.getText() + " (+) ");
                            label4.setForeground(Color.BLUE);
                            label4.setText("Uw gok moet hoger zijn dan " + gok);
                        }

                        // boodschap hoger/lager!
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Uw gok zat niet binnen het interval 0 - " + BOVENGRENS);
                }
            }
        });

        c.setBackground(Color.LIGHT_GRAY);

        allpanel.add(panel1);allpanel.add(panel2);allpanel.add(panel3);

        c.add(allpanel);
        c.add(imagepanel);

        panel1.add(label1);
        panel1.add(veld1);
        panel1.add(button1);

        panel2.add(label2);
        panel2.add(veld2);
        panel2.add(button2);

        panel3.add(label3);
        panel3.add(label4);

        setLocation(100,100);
        setSize(1000, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}